import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
// import { ElMessage } from 'element-plus'
// import { isMobile } from '@conferer/util'

export interface HttpResponse<T = unknown> {
  data: T
  code: string
  status: string
  message?: string
}
const isMobile = false
const api: AxiosInstance = axios.create({
  headers: {
    'Content-type': 'application/json',
  },
})

api.interceptors.request.use((req: AxiosRequestConfig) => {
  return req
})

const notifyMsg = (data: Record<string, any>): void => {
  console.log(data)

  const msg: string = data.message
  if (isMobile) {
    console.log(msg)
    // ElMessage.error(msg)
  } else {
    console.log(msg)
    // ElMessage.error(msg)
  }
}
api.interceptors.response.use(
  (res: AxiosResponse<HttpResponse>) => {
    console.log(res)
    if (res.data?.status === 'success') {
      return res.data
    }

    notifyMsg(res.data)
    return Promise.reject(res)
  },
  (err: any) => {
    console.log('err:', err)

    notifyMsg(err.response.data)
    return Promise.reject(err.response)
  },
)
export default api
